package com.briup.day14;

import java.util.Arrays;

public class SortUtil {
    
	//不指定排序规则，默认升序
	public static void sort(int[] array) {   
		sort(array, true);
	}
	
	//升序or降序
	public static void sort(int[] array, boolean rule) {   
		//rule为true升序，反之，降序
		for(int i=0; i<array.length-1; i++) { 
			for(int j=0; j<array.length-i-1; j++) {  
				if(rule && array[j] > array[j+1] || !rule && array[j] < array[j+1]) {
					swap(array, j, j+1);
			    }
		}
	}
		print(array);
}
	
	
	
	   //交换数组中两元素方法
	   public static void swap(int[] array, int m, int n) {
			int temp = array[m];
			array[m] = array[n];
			array[n] = temp;
		}
		
	    //打印输出排序后数组元素
		public static void print(int[] array) {
			System.out.println(Arrays.toString(array));
		}
		
		public static void main(String[] args) {
			// TODO Auto-generated method stub
			int[] array = {63,45,18,89,57,93,20,35,41,76};
			System.out.println("排序前数组：");
			print(array);
			System.out.println("默认排序数组：");
			sort(array);
			System.out.println("降序排序数组：");
			sort(array, false);
			System.out.println("升序排序数组：");
			sort(array, true);
		}

}
